package com.his.struts.dao.curevideo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.his.struts.form.VideoForm;
import com.his.struts.util.DbUtil;
import com.his.struts.util.PageUtil;

/**
 * 实现视频持久层类
 * 
 * @author mlxnle
 * 
 */
public class CVideoImp implements CVideoDao {
	Logger log = Logger.getLogger(CVideoImp.class);
	ResultSet rset = null;
	DbUtil db = new DbUtil();
	PageUtil pgdb = new PageUtil();

	/**
	 * 添加医疗视频
	 * 
	 * @param videoForm
	 * @return boolean
	 */
	public boolean addVideo(VideoForm videoForm) {
		String sql = "insert into curevideo (vname,vurl)values (?,?)";
		ArrayList list = new ArrayList();
		String name = videoForm.getVname();
		String url = videoForm.getVurl();
		list.add(name);
		list.add(url);
		try {
			db.executeUpdate(sql, list);
			db.closeAll();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return false;
	}

	/**
	 * 删除医疗一个视频
	 * 
	 * @param vid
	 * @return boolean
	 */
	public boolean deleteVideo(int vid) {
		List<Object> list = new ArrayList<Object>();
		list.add(vid);
		String sql = "delete from curevideo where vid = ?";
		try {
			db.executeUpdate(sql, list);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		} finally {
			db.closeAll();
		}
		return false;
	}

	/**
	 * 获得视频列表
	 * 
	 * @return ArrayList<VideoForm>
	 */
	public ArrayList<VideoForm> getVideoList(HttpServletRequest request) {
		VideoForm video = null;
		PageUtil vpdao = new PageUtil();
		ArrayList<VideoForm> list = new ArrayList<VideoForm>();
		rset = vpdao.getPage("curevideo", 8, request);
		if (null != rset) {
			try {
				while (rset.next()) {
					video = new VideoForm();
					int vid = rset.getInt("vid");
					String vurl = rset.getString("vurl");
					String name = rset.getString("vname");
					video.setVid(vid);
					video.setVurl(vurl);
					video.setVname(name);
					list.add(video);
				}
			} catch (SQLException e) {
				log.error(e.getMessage());
			} finally {
				db.closeAll();
			}
		}
		return list;
	}

	/**
	 * 修改视频
	 * 
	 * @param videoForm
	 * @return boolean
	 */
	public boolean updateVideo(VideoForm videoForm) {
		ArrayList list = new ArrayList();
		String name = videoForm.getVname();
		String url = videoForm.getVurl();
		int vid = videoForm.getVid();
		list.add(name);
		list.add(url);
		list.add(vid);
		String sql = "update curevideo set vname = ?, vurl =? where vid = ?";
		try {
			db.executeUpdate(sql, list);
			db.closeAll();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return false;
	}

}
